var isInitData = true;
var isInit = true;
var isInitRight = true;
var selectLeftData = '', selectRightData = '';

$(function () {
    regValidate(["#_bizDataForm"]);
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openInfoChildBizPage();
        },
    }
});

function getLeftParams(params) {
    var destType = $('#destType').val();
    var bizName = $('#bizName').val();
    var isvPid = $('#isvPid').val();
    var bizCode = $('#bizCode').val();
    var cityCode = $('#cityCode').val();
    var deptCode = $('#deptCode').val();
    var pageSize = params.pageSize;
    var pageNo = params.pageNumber;
    if (isInit) {
        isInit = false;
        pageSize = searchInfo ? searchInfo.pageSize : params.pageSize;
        pageNo = searchInfo ? searchInfo.pageNo : params.pageNumber;
    }
    return {
        bizName : bizName,
        isvPid : isvPid,
        bizCode : bizCode,
        cityCode : cityCode,
        deptCode : deptCode,
        destType : destType,
        pageSize : pageSize,
        pageNo : pageNo,
    };
}

function getRightParams(params) {
    var destType = $('#destType').val();
    var bizName = $('#bizName').val();
    var status = $('#status').val();
    var columnName = $('#columnName').val();
    var isvPid = $('#isvPid').val();
    var bizCode = $('#bizCode').val();
    var cityCode = $('#cityCode').val();
    var deptCode = $('#deptCode').val();
    var pageSize = params.pageSize;
    var pageNo = params.pageNumber;
    var outputId = selectLeftData ? selectLeftData.id : '';
    if (isInitRight) {
        isInitRight = false;
        pageSize = searchInfo ? searchInfo.pageSizeCol : params.pageSizeCol;
        pageNo = searchInfo ? searchInfo.pageNoCol : params.pageNumber;
        outputId = searchInfo.id ? searchInfo.id : '';
    }
    return {
        bizName : bizName,
        status : status,
        columnName : columnName,
        isvPid : isvPid,
        bizCode : bizCode,
        cityCode : cityCode,
        deptCode : deptCode,
        destType : destType,
        pageSize : pageSize,
        pageNo : pageNo,
        outputId : outputId,
    };
}

function doRefresh() {
    doLeftRefresh();
    doRightRefresh();
}

function doLeftRefresh() {
    selectLeftData = '';
    var destType = $('#destType').val();
    var bizName = $('#bizName').val();
    var columnName = $('#columnName').val();
    var isvPid = $('#isvPid').val();
    var bizCode = $('#bizCode').val();
    var cityCode = $('#cityCode').val();
    var deptCode = $('#deptCode').val();
    $('#leftTable').bootstrapTable('refresh', {
        url: basePath + 'output/pageFindOutputData?t=' + new Date().getTime(),
        query: {
            bizName : bizName,
            columnName : columnName,
            isvPid : isvPid,
            bizCode : bizCode,
            cityCode : cityCode,
            deptCode : deptCode,
            destType : destType,
        },
    });
}

function doRightRefresh() {
    selectRightData = '';
    var destType = $('#destType').val();
    var bizName = $('#bizName').val();
    var status = $('#status').val();
    var columnName = $('#columnName').val();
    var isvPid = $('#isvPid').val();
    var bizCode = $('#bizCode').val();
    var cityCode = $('#cityCode').val();
    var deptCode = $('#deptCode').val();
    var outputId = selectLeftData ? selectLeftData.id : '';
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'output/pageFindColumnData?t=' + new Date().getTime(),
        query: {
            bizName : bizName,
            status : status,
            columnName : columnName,
            isvPid : isvPid,
            bizCode : bizCode,
            cityCode : cityCode,
            deptCode : deptCode,
            destType : destType,
            outputId : outputId,
        },
    });
}

function createOutputLink() {
    if (!selectLeftData || selectLeftData == '') {
        toastr.error('请先选择一条记录后再进行操作');
        return;
    }
    var postData = {
        destType : selectLeftData.destType,
        bizCode : selectLeftData.bizCode,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'output/createOutputLink',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.result){
                swalInfoToastr('', '链接： ' + data.content);
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.error('生成链接失败');
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function openAddOutputPage() {
    openEditPage('add', 0);
}

function openEditOutputPage() {
    if (!selectLeftData || selectLeftData == '') {
        toastr.error('请先选择一条记录后再进行编辑');
        return;
    }
    openEditPage('edit', selectLeftData.id);
}

function openEditPage(command, id) {
    var tableOptions = $('#leftTable').bootstrapTable('getOptions', null);
    var tableOptionsCol = $('#dataTable').bootstrapTable('getOptions', null);
    var searchStr = $('#searchForm').serialize().replace(/&/g, ',') +
        ',pageNo=' + tableOptions.pageNumber + ',pageSize=' + tableOptions.pageSize +
        ',pageNoCol=' + tableOptionsCol.pageNumber + ',pageSizeCol=' + tableOptionsCol.pageSize;
    window.location.href = basePath + 'output/editOutputPage?editType=' + command +
        '&id=' + id +
        '&t=' + new Date().getTime() +
        '&searchStr=' + searchStr;
}

function deleteOutput() {
    if (!selectLeftData || selectLeftData == '') {
        toastr.error('请先选择一条记录后再进行操作');
        return;
    }
    var postData = {
        outputId : selectLeftData.id,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'output/countColumnByOutput',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.result){
                if (data.code == 'EXIST') {
                    toastr.error('该业务下存在子栏目，无法删除');
                    return;
                }
                var strHtml = '删除后，选择的业务将从当前平台消失，确认删除吗？';
                swalToastr('', strHtml, 'warning', doDelOutput);
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function doDelOutput() {
    var postData = {
        id : selectLeftData.id,
        status : 'deleted',
    };
    $.ajax({
        type: 'post',
        url: basePath + 'output/changeOutputState',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.result){
                toastr.success('删除成功');
                doRefresh();
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function openAddColumnPage() {
    if (!selectLeftData || selectLeftData == '') {
        toastr.error('请先选择输出业务再添加栏目');
        return;
    }
    editColumnPage('add', 0, selectLeftData.id);
}

function openEditColumnPage() {
    if (!selectRightData || selectRightData == '') {
        toastr.error('请先选择一条记录后再进行编辑');
        return;
    }
    var outputId = selectLeftData ? selectLeftData.id : 0;
    editColumnPage('edit', selectRightData.id, outputId);
}

function editColumnPage(command, columnId, outputId) {
    var tableOptions = $('#leftTable').bootstrapTable('getOptions', null);
    var tableOptionsCol = $('#dataTable').bootstrapTable('getOptions', null);
    var searchStr = $('#searchForm').serialize().replace(/&/g, ',') +
        ',pageNo=' + tableOptions.pageNumber + ',pageSize=' + tableOptions.pageSize +
        ',pageNoCol=' + tableOptionsCol.pageNumber + ',pageSizeCol=' + tableOptionsCol.pageSize + ',id=' + outputId;
    window.location.href = basePath + 'output/editColumnPage?editType=' + command +
        '&id=' + columnId +
        '&outputId=' + outputId +
        '&t=' + new Date().getTime() +
        '&searchStr=' + searchStr;
}

function deleteColumn() {
    if (!selectRightData || selectRightData == '') {
        toastr.error('请先选择一条记录后再进行操作');
        return;
    }
    var strHtml = '删除后，选择的栏目将从当前平台消失，确认删除吗？';
    swalToastr('', strHtml, 'warning', doDelColumn);
}

function doDelColumn() {
    var postData = {
        id : selectRightData.id,
        columnCode : selectRightData.columnCode,
        status : 'deleted',
    };
    $.ajax({
        type: 'post',
        url: basePath + 'output/changeColumnState',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.result){
                toastr.success('删除成功');
                doRightRefresh();
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function setState() {
    if (!selectRightData || selectRightData == '') {
        toastr.error('请先选择一条记录后再进行操作');
        return;
    }
    if (selectRightData.status == 'online') {
        var strHtml = '下架后，前端数据变动，确认下架？';
        swalToastr('', strHtml, 'warning', doSetState);
    } else {
        doSetState();
    }
}

function doSetState() {
    var status = 'online';
    var strHtml = '上架';
    if (selectRightData.status == 'online') {
        status = 'offline';
        strHtml = '下架';
    }
    var postData = {
        id : selectRightData.id,
        columnCode : selectRightData.columnCode,
        status : status,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'output/changeColumnState',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.result){
                toastr.success(strHtml + '成功');
                doRightRefresh();
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.error(strHtml + '失败');
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

//左边选中事件
$("#leftTable").on("click-row.bs.table",function(e,row,ele){
    $("#leftTable .success").removeClass("success");
    $(ele).addClass("success");
    selectLeftData = row;
    doRightRefresh();
});
//右边选中事件
$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $("#dataTable .success").removeClass("success");
    $(ele).addClass("success");
    selectRightData = row;
    if (row.status == 'online') {
        $('#_setStateBtn').html('下架');
    } else {
        $('#_setStateBtn').html('上架');
    }
});

function initCheck(value, row, index) {
    if (isInitData && row.id == searchInfo.id) {
        setTimeout(function(){
            $("#leftTable > tbody > tr[data-index='" +index+"']").addClass("success");
        },200);
        isInitData = false;
        selectLeftData = row;
    }
    return value;
}

function formatStatus(value, row, index) {
    if (statusMap) {
        return statusMap[value];
    }
    return value;
}

function formatGrade(value, row, index) {
    if (row.parentCode && row.parentCode != '') {
        return '二级';
    } else {
        return '一级';
    }
}

function destTypeFormat(value, row, index) {
    if (destTypeMap) {
        for (var i in destTypeMap) {
            if (value == destTypeMap[i].code) {
                return destTypeMap[i].name;
            }
        }
    }
    return value;
}